﻿Imports System.IO
Imports TreeCreeper01Interfaces

Friend Class cTaxonomicOverrideReader

    Private mInterdistances(,) As Single
    Private mSpeciesNames() As String
    Private mCountOfSpecies As Integer

    Public Sub WriteToLookup(ByVal Table As ITaxonomicDistanceLookupTable)

        Dim SpeciesIndexBaseOne As Integer
        Dim OtherSpeciesIndexBaseOne As Integer
        Dim InterdistanceNumber As Single

        For SpeciesIndexBaseOne = 1 To mCountOfSpecies
            For OtherSpeciesIndexBaseOne = 1 To mCountOfSpecies
                InterdistanceNumber = mInterdistances(SpeciesIndexBaseOne, OtherSpeciesIndexBaseOne)
                Table.SetInterSpeciesDistance(mSpeciesNames(SpeciesIndexBaseOne), mSpeciesNames(OtherSpeciesIndexBaseOne), InterdistanceNumber)
            Next
        Next

    End Sub

    Public Sub New(ByVal Filename As String)

        Dim FileStreamer As FileStream
        Dim Reader As StreamReader
        Dim OneLine As String
        Dim SpeciesIndexBaseOne As Integer
        Dim Comparitors() As String
        Dim SpeciesName As String
        Dim OtherSpeciesIndexBaseOne As Integer
        Dim InterdistanceString As String
        Dim InterdistanceNumber As Single

        FileStreamer = New FileStream(Filename, FileMode.Open)
        Reader = New StreamReader(FileStreamer)

        OneLine = Reader.ReadLine()
        mSpeciesNames = OneLine.Split(CChar(vbTab))
        mCountOfSpecies = mSpeciesNames.GetUpperBound(0)

        ReDim mInterdistances(mCountOfSpecies, mCountOfSpecies)

        For SpeciesIndexBaseOne = 1 To mCountOfSpecies
            OneLine = Reader.ReadLine
            Comparitors = OneLine.Split(CChar(vbTab))
            SpeciesName = Comparitors(0)
            Debug.Assert(SpeciesName = mSpeciesNames(SpeciesIndexBaseOne))
            For OtherSpeciesIndexBaseOne = 1 To mCountOfSpecies
                InterdistanceString = Comparitors(OtherSpeciesIndexBaseOne)
                InterdistanceNumber = CSng(Val(InterdistanceString))
                If SpeciesIndexBaseOne = OtherSpeciesIndexBaseOne Then
                    Debug.Assert(InterdistanceNumber = 0)
                End If
                mInterdistances(SpeciesIndexBaseOne, OtherSpeciesIndexBaseOne) = InterdistanceNumber
            Next
        Next

        'For SpeciesIndexBaseOne = 1 To mCountOfSpecies
        '    SpeciesName = mSpeciesNames(SpeciesIndexBaseOne)
        '    SpeciesName = Replace(SpeciesName, "_", " ")
        '    mSpeciesNames(SpeciesIndexBaseOne) = SpeciesName
        'Next

        Reader.Close()
        FileStreamer.Close()

        'Integrity check. We have read in the entire rectangle of values
        For SpeciesIndexBaseOne = 1 To mCountOfSpecies
            For OtherSpeciesIndexBaseOne = 1 To mCountOfSpecies
                InterdistanceNumber = mInterdistances(SpeciesIndexBaseOne, OtherSpeciesIndexBaseOne)
                Debug.Assert(InterdistanceNumber = mInterdistances(OtherSpeciesIndexBaseOne, SpeciesIndexBaseOne))
            Next
        Next

        'DumpOne()

    End Sub

    Private Structure Foo
        Public SpeciesName As String
        Public IndexInMainArray As Integer
    End Structure


    'Private Sub DumpOne()

    '    Dim FileStreamer As FileStream
    '    Dim Writer As StreamWriter
    '    Dim SpeciesIndexBaseOne As Integer
    '    Dim Foos(7) As Foo
    '    Dim FooIndex As Integer
    '    Dim OtherFoo As Integer
    '    Dim Interdistance As Single
    '    Dim OutString As String

    '    Foos(1).SpeciesName = "Abutilon_theophrasti"
    '    Foos(2).SpeciesName = "Acer_pseudoplatanus"
    '    Foos(3).SpeciesName = "Mercurialis_perennis"
    '    Foos(4).SpeciesName = "Micromeria_juliana"
    '    Foos(5).SpeciesName = "Moenchia_erecta"
    '    Foos(6).SpeciesName = "Minuartia_mediterranea"
    '    Foos(7).SpeciesName = "Moehringia_trinervia"

    '    For FooIndex = 1 To 7
    '        With Foos(FooIndex)
    '            For SpeciesIndexBaseOne = 1 To mCountOfSpecies
    '                If .SpeciesName = mSpeciesNames(SpeciesIndexBaseOne) Then
    '                    Foos(FooIndex).IndexInMainArray = SpeciesIndexBaseOne
    '                    Exit For
    '                End If
    '            Next
    '        End With
    '    Next

    '    FileStreamer = New FileStream("D:\Mediterranean\TreeCreeper\Documents and Testing\Spatial10.txt", FileMode.Create)
    '    Writer = New StreamWriter(FileStreamer)

    '    Writer.Write(".")
    '    For FooIndex = 1 To 7
    '        Writer.Write(vbTab)
    '        Writer.Write(Foos(FooIndex).SpeciesName)
    '    Next

    '    For FooIndex = 1 To 7
    '        Writer.WriteLine()
    '        Writer.Write(Foos(FooIndex).SpeciesName)
    '        For OtherFoo = 1 To 7
    '            Interdistance = mInterdistances(Foos(FooIndex).IndexInMainArray, Foos(OtherFoo).IndexInMainArray)
    '            Writer.Write(vbTab)
    '            OutString = Format(Interdistance, "##000.000000")
    '            OutString = OutString.Replace(",", ".")
    '            Writer.Write(OutString)
    '        Next
    '    Next

    '    Writer.Close()

    'End Sub

End Class
